Loops
A loop statement allows programmers to execute a statement or group of statements multiple times without repetition of code.
Type of Loop
A Loop can be categories on the basis of:
- The nature of control variable and the kind of value assigned
- Depending on the position of the control statement in the loop
- Based on the nature of control variable and the kind of value assigned to it for testing the control expression, the kind of value assigned to it for testing the control expression, the loops may be classified into two general categories:
Counter-Controlled Loop
- Counter controlled loops is also known as definite repetition loop
- In Counter controlled loop the number of iterations is known before the loop begins to execute.
- The counter-controlled loop has the following components:
- a control variable
- the increment (or decrement) value by which the control variable is modified at each iteration of the loop.
- t he loop terminating condition that checks if looping should continue.
-
Example |
#include < stdio.h>
int main()
{
int i = 2;
while(i < 10)
{
printf(“Hello World- %d\n”, i);
i++;
}
return 0;
}
|
Sentinel - Controlled Loop
- Sentinel controlled loops is also known as indefinite repetition loop
- In Sentinel controlled loop the number of iterations is not known before the loop begins to execute.
- In a sentinel-controlled loop, a special value called sentinel value is used to change the loop control expression from true to false in order to determine whether to execute the loop body.
- Sentinel controlled loop is useful when we don’t know in advance how many times the loop will be executed.
-
Example |
#include < stdio.h>
void main()
{
do
{
printf(“Input a number:”);
scanf(“%d”, &num);
}
while(num > 0);
}
|
- Based on the nature of control variable and the kind of value assigned to it for testing the control expression, the kind of value assigned to it for testing the control expression, the loops may be classified into two general categories:
Entry-Controlled Loop
- In the entry-controlled loop, the control conditions are tested before the start of loop execution.
- If the conditions are not satisfied, then the body of the loop will not be executed.
- Also known as pre-test loop.
-
Exit-Controlled Loop
- In exit-controlled loop, the test is performed at the end of the body of the loop and therefore the body is executed unconditionally for the first time.
- Also known as post-test loop.
-
A looping process include the following four steps:
- Setting and initialization of a condition variable.
- Execution of the statements in the loop.
- Test for a specified value of the condition variable for execution of the loop.
- Increment or updating the condition variable.
while loop
- The while is an entry-controlled loop statement.
- The test-condition is evaluated and if the condition is true, then the body of the loop is executed.
- In while loop the execution is terminated on the basis of the test condition. If the test condition will become false then it will break from the while loop else body will be executed.
-
Example |
#include < stdio.h>
void main()
{
int sum = 0;
int n = 1;
while(n <=10)
{
sum = sum + n * n;
n = n + 1;
printf(“%d\n”, sum);
}
}
|
for loop
- The while is an entry-controlled loop statement.
- The for loop follows a very structured approach where it begins with initializing a
condition then checks the condition and in the end conditional statements followed
by an updation of values.
-
where
Initialization
|
- |
This step setting a loop counter to an initial value
|
text-condition
|
- |
This step testing the loop counter to
determine whether its value has reached the number of repetitions desired.
|
increment/decrement
|
- |
This step increasing/decreasing the value of
loop counter each time the program segment
within the loop has been executed
|
Example |
#include < stdio.h>
int main()
{
for(int i=2; i<10; i++)
{
printf(“Hello world- %d\n”,i);
}
return 0;
}
|
do...while loop
- The do….while is an exit-controlled loop statement.
- do…while is a loop in which a set of instruction will execute at least once (irrespective of the condition) and then the repetition of loop’s body will depend on the condition passed at the end of the loop in the while() function.
-
Example |
#include <stdio.h>
int main()
{
int i=0;
do
{
printf("Hello world- %d\n", i);
i++;
}
while(i<10);
return 0;
}
|
Break statement
- The break is a keyword in C language which is used to bring the program control out of the loop or switch statement.
- The break statement breaks the loop one by one i.e., in the case of nested loop, it breaks the inner loop first and then proceed to order loops.
-
Example |
#include < stdio.h>
int main()
{
for(int i=2; i<10; i++)
{
if(i=5)
{
break;
}
printf(“Hello world- %d\n”, i);
}
printf(“break loop at- %d\n”, i);
return 0;
}
|
continue statement
- The continue statement in C language is used to bring the program control to the beginning of the loop.
- The continue statement skips some lines of code inside the loop and continues with the next iteration.
-
Example |
#include < stdio.h>
int main()
{
int i=1;
for(i<10; i++)
{
if(i=5)
{
continue;
}
}
printf(“%d\n”, i);
return 0;
}
|